#
# PREREQUISITES
#
# apt-get install linux-headers-$(uname -r)
# apt-get install build-essential devscripts ubuntu-dev-tools debhelper dh-make diff patch cdbs quilt gnupg fakeroot lintian  pbuilder piuparts
#

ifeq (,$(BUILD_KERNEL))
BUILD_KERNEL=$(shell uname -r)
endif
HERE=$(shell pwd)
PF_RING_HOME=$(HERE)/../../

all: clean @PRE_DEP@ build

pre-drivers: pre-zc

pre-kernel-nodrivers:
	cd ../../kernel; ./configure; sudo make -f Makefile.dkms deb

pre-kernel: pre-kernel-nodrivers pre-drivers

pre-zc:
	cd ../../drivers/intel/; ./configure; make dkms-deb
	cp ../../drivers/intel/*.deb .

pre: pre-kernel pre-build
pre-nodrivers: pre-kernel-nodrivers pre-build

pre-build: pre-build-core @PRE_BUILD_EXAMPLES@

pre-build-core:
	-mv *.deb packages
	cat /dev/null > debian/files
	@echo "************************************************************"
	@echo "- REMEMBER to upgrade dependency in debian/control"
	@echo "- REMEMBER to set the kernel version in debian/control"
	@echo "- Make sure you have updated the version in debian/changelog"
	@echo "************************************************************"
	rm -rf $(HERE)/debian/pfring
	mkdir -p debian/lib debian/usr/include/linux debian/usr/bin debian/usr/lib debian/usr/local/lib debian/usr/local/bin @LIBBPF_DEST@
	cd $(PF_RING_HOME)/userland; ./configure --includedir=/usr/include --libdir=/usr/lib; make
	#cd $(PF_RING_HOME)/userland/snort/pfring-daq-module; autoreconf -ivf; ./configure; make; mkdir -p $(HERE)/debian/usr/local/lib/daq; cp $(PF_RING_HOME)/userland/snort/pfring-daq-module/.libs/daq_pfring.la $(PF_RING_HOME)/userland/snort/pfring-daq-module/.libs/daq_pfring.so $(HERE)/debian/usr/local/lib/daq
	#cd $(PF_RING_HOME)/userland/snort/pfring-daq-module-zc; autoreconf -ivf; ./configure; make; mkdir -p $(HERE)/debian/usr/local/lib/daq; cp $(PF_RING_HOME)/userland/snort/pfring-daq-module-zc/.libs/daq_pfring_zc.la $(PF_RING_HOME)/userland/snort/pfring-daq-module-zc/.libs/daq_pfring_zc.so $(HERE)/debian/usr/local/lib/daq
	cd $(PF_RING_HOME)/userland/lib; make install DESTDIR=$(HERE)/debian
	cd $(PF_RING_HOME)/userland/libpcap; ./configure; make; cp libpcap* $(HERE)/debian/usr/local/lib/; cd $(HERE)/debian/usr/local/lib/; ln -sf $(shell ls $(PF_RING_HOME)/userland/libpcap/libpcap.so.* | xargs -n 1 basename) libpcap.so
	cd $(PF_RING_HOME)/kernel/linux/; cp pf_ring.h $(HERE)/debian/usr/include/linux/
	mkdir -p $(HERE)/debian/usr/lib/x86_64-linux-gnu/wireshark/extcap
	cp $(PF_RING_HOME)/userland/wireshark/extcap/ntopdump $(HERE)/debian/usr/lib/x86_64-linux-gnu/wireshark/extcap
	@LIBBPF_COPY@
	@find $(HERE)/debian -name "*~" -exec /bin/rm {} ';' 

pre-build-examples:
	cd $(PF_RING_HOME)/userland/tcpdump; ./configure; make; cp tcpdump $(HERE)/debian/usr/local/bin
	cd $(PF_RING_HOME)/userland/examples; cp pfcount pfsend pfcount_multichannel pfsend_multichannel pfwrite pfbridge $(HERE)/debian/usr/bin
	cd $(PF_RING_HOME)/userland/examples_zc; cp zbalance_ipc zsend zcount zcount_ipc $(HERE)/debian/usr/bin
	cd $(PF_RING_HOME)/userland/examples_ft; cp ftflow $(HERE)/debian/usr/bin
	cd $(PF_RING_HOME)/tools; cp n2if $(HERE)/debian/usr/bin

build-nodrivers: pre-kernel-nodrivers build

build: pre-build
	dpkg-buildpackage -rfakeroot -b -us -uc #-d
	@\rm -f ../pf_ring_*dsc ../pf_ring_*.gz ../pf_ring_*changes 
	echo "Signing package"
	@SIGN_CMD@ --sign builder -k D1EB60BE ../pfring_*deb
	@/bin/mv ../pfring_*deb . 
	@echo 
	@echo "Package built." 
	@/bin/ls  pfring_*deb 
	@echo "-------------------------------" 
	-dpkg --contents  pfring_*deb 
	@echo "-------------------------------" 
	@cp /var/lib/dkms/pfring/@PFRING_VERS@.@REVISION@/deb/pfring-dkms_@PFRING_VERS@.@REVISION@_all.deb .
	-dpkg --contents  pfring-dkms*deb 
	@echo "-------------------------------" 


clean:
	@\rm -f ./*.deb
	-rm -rf debian/pfring
	-rm -rf debian/usr
	-rm -rf debian/lib
